from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO
import logging

# 导入统一的日志配置模块
from log_config import setup_logging

# 配置日志
setup_logging()
logger = logging.getLogger(__name__)


class HTMLProcessor:
    def __init__(self):
        pass
    
    def load_html(self, html_path_or_url):
        try:
            if html_path_or_url.startswith(('http://', 'https://')):
                response = requests.get(html_path_or_url, timeout=10)
                response.raise_for_status()  # 抛出HTTP错误
                return response.text
            else:
                with open(html_path_or_url, 'r', encoding='utf-8') as f:
                    return f.read()
        except Exception as e:
            logger.error(f"加载HTML失败: {str(e)}")
            return ""
    
    def extract_images(self, html_content):
        if not html_content:
            return []
            
        soup = BeautifulSoup(html_content, 'html.parser')
        img_tags = soup.find_all('img')
        images = []
        for img in img_tags:
            img_info = {
                'url': img.get('src', ''),
                'alt': img.get('alt', 'No description available'),
                'title': img.get('title', '')
            }
            images.append(img_info)
        return images
    
    def extract_text_with_layout(self, html_content):
        if not html_content:
            return ""
            
        soup = BeautifulSoup(html_content, 'html.parser')
        for script in soup(["script", "style", "noscript", "iframe"]):
            script.decompose()
            
        text_parts = []
        # 处理主要文本标签
        for tag in soup.find_all(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'div']):
            text = tag.get_text(strip=True)
            if text and len(text) > 1:  # 过滤过短的文本
                text_parts.append(f"<{tag.name}>{text}</{tag.name}>")
        
        return '\n\n'.join(text_parts)
    
    def get_image_metadata(self, img_src):
        """处理图像，返回某个图像元信息"""
        try:
            if not img_src:
                logger.warning("图像源为空")
                return None                
               
            if img_src.startswith(('http://', 'https://')):
                response = requests.get(img_src, timeout=5)
                response.raise_for_status()
                img = Image.open(BytesIO(response.content))
            else:
                img = Image.open(img_src)
                
            img_info = {
                'format': img.format,
                'size': img.size,
                'mode': img.mode,
                'description': f"Image of format {img.format} with size {img.size}"
            }
            return img_info
        except Exception as e:
            logger.error(f"处理图像 {img_src} 时出错: {str(e)}")
            return None
    
    def parse(self, html_path_or_url, target=['text', 'image']):
        try:
            html_content = self.load_html(html_path_or_url)
            if not html_content:
                logger.warning("解析后html 内容为空")
                return { 'success': False, 'message': "解析后html 内容为空" }
            text_content = ''
            html_images = []
            if 'text' in target:
                text_content = self.extract_text_with_layout(html_content)
            if 'image' in target:
                html_images = self.extract_images(html_content)

            # 处理图像嵌入
            images = []
            for img in html_images:
                img_path = img['url']
                img_data = self.get_image_metadata(img_path)
                if img_data:
                    img_metadata = {**img,** img_data}
                images.append({"img_path": img_path,"metadata": img_metadata})

            return {
                'success': True,
                'text_content': text_content,
                'images': images
            }
        except Exception as e:
                return { 'success': False, 'message': str(e) }            

 # 使用示例
if __name__ == "__main__":
    processor = HTMLProcessor()
    html_source = "https://www.17fpv.com/842.html"
    result = processor.parse(html_source, ['text', 'image'])
    print(result['text_content'])
    print("")
